package com.example.a2212a_park

import android.os.CountDownTimer
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.a2212a_park.databinding.ActivitySplashBinding
import com.example.base.app.path.RouterPath
import com.example.common.view.adapter.ImageBannerAdapter
import com.example.common.view.entity.BannerEntity
import com.example.common.view.view.BaseMvvmActivity
import com.example.image.glide.GlideImgLoader
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer
import dagger.hilt.android.AndroidEntryPoint

/**
 * 作者：衣晓佳
 * 日期：2025/08/16
 * 描述：游戏鸿蒙-智慧园区-01-启动页
 * 业务：1.倒计时；2.APP主路
 */
@AndroidEntryPoint
class SplashActivity : BaseMvvmActivity<ActivitySplashBinding>() {
    private val bannerData = mutableListOf<BannerEntity>()
    private lateinit var mBannerAdapter:ImageBannerAdapter
    private lateinit var mCountDownTimer: CountDownTimer
    override fun initView() {
        val isFirst = MMKV.defaultMMKV().decodeBool("isFirstBoot", true)
        if(isFirst){
            MMKV.defaultMMKV().encode("isFirstBoot",false)
            showAppBanner()
        }else{
            showAppAdv()
        }
    }

    /**
     * 展示App主图
     */
    private fun showAppBanner(){
        //View.VISIBLE表示显示UI View.INVISIBLE表示不显示UI,但是可以占据UI大小和位置 View.GONE表示既不显示UI也不占据大小和位置
        mDataBinding.clAppBanner.visibility = View.VISIBLE
        mDataBinding.clAppAdv.visibility = View.GONE
        bannerData.run {
            add(BannerEntity(0,R.drawable.back,""))
            add(BannerEntity(1,R.drawable.back,""))
            add(BannerEntity(2,R.drawable.back,""))
        }
        mBannerAdapter = ImageBannerAdapter(bannerData)
        mDataBinding.appBanner.run {
            setAdapter(mBannerAdapter)
            //关联生命周期
            addBannerLifecycleObserver(this@SplashActivity)
            //设置指示器
            indicator = CircleIndicator(this@SplashActivity)
            //设置切换效果
            addPageTransformer(AlphaPageTransformer())
            //设置页面切换的监听
            addOnPageChangeListener(object :OnPageChangeListener{
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {}

                override fun onPageSelected(position: Int) {
                    //判断Banner是否滑动到最后一页
                    if(position == bannerData.size - 1){
                        mDataBinding.tvStart.visibility = View.VISIBLE
                    }else{
                        mDataBinding.tvStart.visibility = View.INVISIBLE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) {}
            })
        }
        //点击立即进入
        mDataBinding.tvStart.setOnClickListener {
            jumpMainActivity()
        }
    }

    /**
     * 展示App广告
     */
    private fun showAppAdv(){
        mDataBinding.clAppBanner.visibility = View.GONE
        mDataBinding.clAppAdv.visibility = View.VISIBLE
        //显示广告图片
        mDataBinding.ivAdv.run {
            GlideImgLoader.loadResImage(this,R.drawable.back,this)
            //5秒倒计时
            mCountDownTimer = object :CountDownTimer(5000,1000){
                override fun onTick(millisUntilFinished: Long) {
                    mDataBinding.tvAdvSkip.text = "${millisUntilFinished / 1000}秒跳过"
                }

                override fun onFinish() {
                    //this@SplashActivity.jumpMainActivity()
                    ARouter.getInstance().build(RouterPath.PATH_MAIN_PAGE).navigation()
                }
            }
        }
        //启动倒计时
        mCountDownTimer?.start()
    }

//    override fun onResume() {
//        super.onResume()
//
//    }
    override fun onDestroy() {
        super.onDestroy()
        mCountDownTimer?.cancel()
    }
    override fun getLayoutId(): Int {
        return R.layout.activity_splash
    }
    private fun jumpMainActivity(){
        ARouter.getInstance().build(RouterPath.PATH_MAIN_PAGE).navigation()
        ToastUtils.showShort("欢迎进入")
    }
}